package com.pukea.app.medical.zyweixin.user;

import sun.jdbc.rowset.CachedRowSet;

import com.puda.mteaf.core.util.Tools;
import com.puda.mteaf.core.util.collapsar.Cipher;
import com.witnsoft.mteaf.core.action.BaseActionSupport;
import com.witnsoft.mteaf.core.event.Event;
import com.witnsoft.mteaf.core.event.EventResponse;
import com.witnsoft.mteaf.core.event.RespData;
import com.witnsoft.mteaf.core.exception.BussinessProcessException;
import com.witnsoft.mteaf.core.util.DBTransUtil;
import com.witnsoft.mteaf.core.util.QueryCondition;

//import com.puda.mteaf.core.util.DBTransUtil;
//import com.witnsoft.mteaf.core.util.DBTransUtil;
//import com.puda.mteaf.core.util.Tools;
//import com.puda.mteaf.core.util.collapsar.Cipher;
//import com.witnsoft.mteaf.core.exception.BussinessProcessException;
import com.puda.mteaf.core.manage.user.*;
import java.sql.ResultSetMetaData;
import java.util.HashMap;
import java.util.Map;

import net.minidev.json.JSONObject;
import net.minidev.json.JSONValue;
import org.apache.log4j.Logger;
//import sun.jdbc.rowset.CachedRowSet;

public class ZyYfUser extends User
{
  public static Logger log = Logger.getLogger(ZyYfUser.class);
  
  public Map<String, String> verifyUser(String username, String password) throws BussinessProcessException
  {
	  
	log.info("-----------------------------------------------ZyYfUser.ZyYfUser 验证药房端登录----------------------");
	  
    DBTransUtil transUtil = new DBTransUtil();
    StringBuffer strSql = new StringBuffer("");
    CachedRowSet crs = null;
    Map<String, String> verifyResult = new HashMap<String, String>();
    verifyResult.put("errCode", "500");
    
    try
    {
      if ((username == null) || (username.equals("")))
      {
    	verifyResult.put("msg", "用户名有误");
        log.info("用户名有误： username = " + username);
        return verifyResult;
      }
      if ((password == null) || (password.equals("")))
      {
      	verifyResult.put("msg", "密码有误");
        log.info("用户名有误： password = " + password);
        return verifyResult;
      }     
      
      strSql = new StringBuffer("select p.*,yd.ydmc as ydmc, yd.ydssqx as ydssqx, yd.ydssqxmc as ydssqxmc, yd.yddz as yddz, yd.yddh as yddh, yd.ydpicurl as ydpicurl,1 as yzflag from pha_info p left join ask_yd yd on p.ayid = yd.ayid where 1 = 1");
      strSql.append(" and phaname = '").append(username).append("'");
	  strSql.append(" and phapassword = '").append(Cipher.Encrypt(password)).append("'");
      log.info("strSql = "+strSql.toString());
      
      
      crs = transUtil.getResultBySelectSun(strSql.toString());
      
      if (crs==null||crs.size()==0) {
    	verifyResult.put("msg", "未注册用户，或输入信息有误");
    	log.info("未注册用户，或输入信息有误");
    	return verifyResult;
      }else{
    	verifyResult.put("errCode", "200");
    	verifyResult.put("msg", GetCrsToJson(crs));
    	log.info("verifyResult : " + verifyResult.toString());
    	return verifyResult;
      }

    }
    catch (Exception fe)
    {
      throw new BussinessProcessException(fe.getMessage());
    }
    
  }
  public static String GetCrsToJson(CachedRowSet crs)
  {
    return GetCrsToJson(crs, false);
  }
  
  public static String GetCrsToJson(CachedRowSet crs, boolean isNull)
  {
    StringBuffer json = new StringBuffer("");
    if (crs == null) {
      return "";
    }
    try
    {
      crs.beforeFirst();
      ResultSetMetaData md = crs.getMetaData();
      int i = 0;
      if (isNull) {
        for (int j = 1; j <= md.getColumnCount(); j++)
        {
          String value = "";
          if (j == 1) {
            value = "";
          }
          if (j != 1) {
            json.append(",");
          }
          json.append("\"").append(md.getColumnName(j).toLowerCase()).append("\":\"" + value + "'");
        }
      }
      while (crs.next())
      {
        if (i != 0) {
          json.append(",");
        }
        if ((i == 0) && (isNull)) {
          json.append(",");
        }
        for (int j = 1; j <= md.getColumnCount(); j++)
        {
          if (j != 1) {
            json.append(",");
          }
          json.append("\"").append(md.getColumnName(j).toLowerCase()).append("\":\"").append(Tools.nvl(crs.getString(md.getColumnName(j)))).append("\"");
        }
        i++;
      }
    }
    catch (Exception fe)
    {
      log.info("error at crsToJson : " + fe.getMessage());
      return "";
    }
    log.info("GetCrsToJson json.toString() = " + json.toString());
    return json.toString();
  }
}
